<?php


namespace app\common\tool;
use Firebase\JWT\JWT;


class JwtAuth
{
    protected $alg;
    protected $key;
    protected $issuer;
    public function __construct(string $alg, string $key, string $issuer = 'DeltaF')
    {
        $this->alg = $alg;
        $this->key = $key;
        $this->issuer = $issuer;
    }

    public function create($data = '', int $nbf = 0, int $exp = null)
    {
        $now = time();

        $payload = [
            'iss' => $this->issuer,
            'aud' => $this->issuer,
            'iat' => $now,
            'nbf' => $now + $nbf,
            'data' => $data
        ];

        if (!empty($exp)) $payload['exp'] = $now + $exp;

        return JWT::encode($payload, $this->key, $this->alg);
    }

    public function verify(string $jwt)
    {
        JWT::decode($jwt, $this->key, [$this->alg]);
    }
}